﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace XNET.Core
{
    /// <summary>
    /// WINAPI
    /// </summary>
    public class XWinAPI
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 写入InI文件
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        /// <param name="filePath">文件路径</param>
        public static void WriteIni(string section, string key, string val, string filePath)
        {
            WritePrivateProfileString(section, key, val, filePath);
        }
        /// <summary>
        /// 读取Ini文件
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static string ReadIni(string section, string key, string filePath)
        {
            if (File.Exists(filePath))
            {
                StringBuilder sTemp = new StringBuilder();
                GetPrivateProfileString(section, key, "", sTemp, 255, filePath);
                return sTemp.ToString();
            }
            else
                return string.Empty;
        }
    }
}
